***************************************************
***************************************************
**********                               ********** 
**********         Vote at 18 			 ********** 
**********                               **********    
***************************************************
***************************************************

global beg_path `" XXX "'  // Indicate your working directory

* Opening the dataset with elections (to get the country-years)

use `"${beg_path}Data\Macro\Base_dataset"', clear
gen country_to_merge = country

* Uploading the UN population data

merge m:1 country_to_merge year using `"${beg_path}Data\Macro\UN_gen.dta"' 
drop _merge
drop if country ==.

* Starting year voting age == 18 (first elections when the 18-year-old were eligible to vote)

gen starting_year_18 =.  // 
replace starting_year_18 = 	1945	if country ==	1	//	Albania
replace starting_year_18 = 	1864	if country ==	2	//	Argentina
replace starting_year_18 = 	1937	if country ==	3	//	Armenia
replace starting_year_18 = 	1974	if country ==	4	//	Australia
replace starting_year_18 = 	1994	if country ==	5	//	Austria
replace starting_year_18 = 	1973	if country ==	6	//	Bangladesh
replace starting_year_18 = 	1918	if country ==	7	//	Belarus
replace starting_year_18 = 	1981	if country ==	8	//	Belgium
replace starting_year_18 = 	.a	if country ==	9	//	Benin
replace starting_year_18 = 	1997	if country ==	10	//	Bolivia
replace starting_year_18 = 	1999	if country ==	11	//	Botswana
replace starting_year_18 = 	1934	if country ==	12	//	Brazil
replace starting_year_18 = 	1946	if country ==	13	//	Bulgaria
replace starting_year_18 = 	.a	if country ==	14	//	Burkina Faso
replace starting_year_18 = 	1993	if country ==	15	//	Burundi
replace starting_year_18 = 	1972	if country ==	16	//	Canada
replace starting_year_18 = 	1975	if country ==	17	//	Cape Verde
replace starting_year_18 = 	1970	if country ==	18	//	Chile
replace starting_year_18 = 	1978	if country ==	19	//	Colombia
replace starting_year_18 = 	1978	if country ==	20	//	Comoros
replace starting_year_18 = 	1974	if country ==	21	//	Costa Rica
replace starting_year_18 = 	1945	if country ==	22	//	Croatia
replace starting_year_18 = 	1998	if country ==	23	//	Cyprus
replace starting_year_18 = 	1946	if country ==	24	//	Czech Republic
replace starting_year_18 = 	1946	if country ==	25	//	Czechoslovakia
replace starting_year_18 = 	1979	if country ==	26	//	Denmark
replace starting_year_18 = 	1874	if country ==	27	//	Dominican Republic
replace starting_year_18 = 	1977	if country ==	28	//	East Timor (Timor-Leste)
replace starting_year_18 = 	1901	if country ==	29	//	Ecuador
replace starting_year_18 = 	1952	if country ==	30	//	El Salvador
replace starting_year_18 = 	1946	if country ==	31	//	Estonia
replace starting_year_18 = 	2014	if country ==	32	//	Fiji
replace starting_year_18 = 	1975	if country ==	33	//	Finland
replace starting_year_18 = 	1978	if country ==	34	//	France
replace starting_year_18 = 	1996	if country ==	35	//	Gambia
replace starting_year_18 = 	1936	if country ==	36	//	Georgia
replace starting_year_18 = 	1972	if country ==	37	//	Germany
replace starting_year_18 = 	1992	if country ==	38	//	Ghana
replace starting_year_18 = 	1985	if country ==	39	//	Greece
replace starting_year_18 = 	1935	if country ==	40	//	Guatemala
replace starting_year_18 = 	1977	if country ==	41	//	Guinea-Bissau
replace starting_year_18 = 	1973	if country ==	42	//	Guyana
replace starting_year_18 = 	1981	if country ==	44	//	Honduras
replace starting_year_18 = 	1953	if country ==	45	//	Hungary
replace starting_year_18 = 	1989	if country ==	46	//	India
replace starting_year_18 = 	1955	if country ==	47	//	Indonesia
replace starting_year_18 = 	1980	if country ==	48	//	Iraq
replace starting_year_18 = 	1977	if country ==	49	//	Ireland
replace starting_year_18 = 	1949	if country ==	50	//	Israel
replace starting_year_18 = 	1976	if country ==	51	//	Italy
replace starting_year_18 = 	1976	if country ==	52	//	Jamaica
replace starting_year_18 = 	2017	if country ==	53	//	Japan
replace starting_year_18 = 	1974	if country ==	54	//	Kenya
replace starting_year_18 = 	1963	if country ==	55	//	Korea South
replace starting_year_18 = 	1945	if country ==	56	//	Kosovo
replace starting_year_18 = 	1936	if country ==	57	//	Kyrgyzstan
replace starting_year_18 = 	1967	if country ==	58	//	Laos
replace starting_year_18 = 	1946	if country ==	59	//	Latvia
replace starting_year_18 = 	1998	if country ==	61	//	Lesotho
replace starting_year_18 = 	1975	if country ==	62	//	Liberia
replace starting_year_18 = 	1946	if country ==	63	//	Lithuania
replace starting_year_18 = 	1974	if country ==	64	//	Luxembourg
replace starting_year_18 = 	1945	if country ==	65	//	Macedonia
replace starting_year_18 = 	1977	if country ==	66	//	Madagascar
replace starting_year_18 = 	1999	if country ==	67	//	Malawi
replace starting_year_18 = 	2021	if country ==	68	//	Malaysia
replace starting_year_18 = 	1992	if country ==	69	//	Mali
replace starting_year_18 = 	1976	if country ==	70	//	Mauritius
replace starting_year_18 = 	1970	if country ==	71	//	Mexico
replace starting_year_18 = 	1946	if country ==	72	//	Moldova
replace starting_year_18 = 	1936	if country ==	73	//	Mongolia
replace starting_year_18 = 	1945	if country ==	74	//	Montenegro
replace starting_year_18 = 	1951	if country ==	75	//	Myanmar (Burma)
replace starting_year_18 = 	1989	if country ==	76	//	Namibia
replace starting_year_18 = 	1991	if country ==	77	//	Nepal
replace starting_year_18 = 	1972	if country ==	78	//	Netherlands
replace starting_year_18 = 	1975	if country ==	79	//	New Zealand
replace starting_year_18 = 	1984	if country ==	80	//	Nicaragua
replace starting_year_18 = 	.a	if country ==	81	//	Niger
replace starting_year_18 = 	1979	if country ==	82	//	Nigeria
replace starting_year_18 = 	1981	if country ==	83	//	Norway
replace starting_year_18 = 	2002	if country ==	84	//	Pakistan
replace starting_year_18 = 	1978	if country ==	85	//	Panama
replace starting_year_18 = 	1870	if country ==	86	//	Paraguay
replace starting_year_18 = 	1980	if country ==	87	//	Peru
replace starting_year_18 = 	1978	if country ==	88	//	Philippines
replace starting_year_18 = 	1952	if country ==	89	//	Poland
replace starting_year_18 = 	1975	if country ==	90	//	Portugal
replace starting_year_18 = 	1948	if country ==	91	//	Romania
replace starting_year_18 = 	1918	if country ==	92	//	Russia
replace starting_year_18 = 	1993	if country ==	93	//	Senegal
replace starting_year_18 = 	1945	if country ==	94	//	Serbia
replace starting_year_18 = 	1945	if country ==	95	//	Serbia and Montenegro
replace starting_year_18 = 	1996	if country ==	96	//	Sierra Leone
replace starting_year_18 = 	.	if country ==	97	//	Singapore // In 2018, voting age is still 21
replace starting_year_18 = 	1946	if country ==	98	//	Slovak Republic
replace starting_year_18 = 	1945	if country ==	99	//	Slovenia
replace starting_year_18 = 	1976	if country ==	100	//	Solomon Islands
replace starting_year_18 = 	1961	if country ==	102	//	South Africa
replace starting_year_18 = 	1979	if country ==	103	//	Spain
replace starting_year_18 = 	1960	if country ==	104	//	Sri Lanka
replace starting_year_18 = 	1965	if country ==	105	//	Sudan
replace starting_year_18 = 	1976	if country ==	106	//	Sweden
replace starting_year_18 = 	1991	if country ==	107	//	Switzerland
replace starting_year_18 = 	.	if country ==	109	//	Taiwan: not adopted as of 2018 (but reduced in referenda to 18 in 2017)
replace starting_year_18 = 	1995	if country ==	110	//	Thailand
replace starting_year_18 = 	1976	if country ==	111	//	Trinidad and Tobago
replace starting_year_18 = 	2009	if country ==	112	//	Tunisia
replace starting_year_18 = 	1995	if country ==	113	//	Turkey
replace starting_year_18 = 	1918	if country ==	115	//	Ukraine
replace starting_year_18 = 	1970	if country ==	116	//	United Kingdom
replace starting_year_18 = 	1972	if country ==	117	//	United States
replace starting_year_18 = 	1919	if country ==	118	//	Uruguay
replace starting_year_18 = 	1958	if country ==	119	//	Venezuela
replace starting_year_18 = 	1945	if country ==	120	//	Yugoslavia
replace starting_year_18 = 	1968	if country ==	121	//	Zambia

*Getting rid of individuals that have not reached the voting age 
foreach var of varlist UN_gen_a_* {
local x = substr("`var'", -2, 2)
replace `var' = . if `x' < Voting_age
}

*Generating total voting age population
egen total_VAP = rowtotal(UN_gen_a_* UN_gen_G_80and_more) 
 
*Generating year of birth
foreach var of varlist UN_gen_a* {
local x = substr("`var'", -2, 2)
gen yob_`var' = year - `x'
}
gen yob_UN_gen_G_80and_more = year - 80

*Setting as missing all those cohorts when voting at 18 was not possible when they reached 18
foreach var of varlist UN_gen_a* {
replace `var' = . if (yob_`var'+18) < starting_year_18
}  
replace UN_gen_G_80and_more = . if (yob_UN_gen_G_80and_more+18) < starting_year_18

*Generating the share of individuals who could vote at 18 
egen voting_age_temp = rowtotal(UN_gen_a_* UN_gen_G_80and_more) 
gen voting_age18_cum = voting_age_temp/total_VAP

*Correcting for missing info 
replace voting_age18_cum  = 0 if voting_age18_cum ==. & year < 1950 & starting_year_18 > 1949 /* The UN population data start only in 1950 but we can code as 0 the pre-1950 years for countries where voting at 18 was adopted after 1949. */
replace voting_age18_cum  = . if starting_year_18 ==.a  // Countries where we do not know
replace voting_age18_cum  = 0 if starting_year_18 ==.  // Countries where vote at 18 not adopted yet (Singapore, Taiwan)

collapse voting_age18_cum starting_year_18, by(country year) // this is to facilitate later merge (only 1 observation per country/year).

lab var voting_age18_cum "Vote at 18" 
lab var starting_year_18 "Starting Year: Vote at 18"

save `"${beg_path}Data\Macro\voting_age18_shares.dta"', replace
 